package cn.bugio.springcloud.controller;

import cn.bugio.springcloud.entity.Student;
import cn.bugio.springcloud.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RequestMapping("/student/")
@RestController
public class StudentHandler {

    @Autowired
    private StudentRepository studentRepository;

    @Value("${server.port}")
    private String post;

    @GetMapping("findAll")
    public Collection<Student> findAll(){
        return studentRepository.findAll();
    }

    @GetMapping("findById/{id}")
    public Student findById(@PathVariable("id") long id){
        return studentRepository.findById(id);
    }

    @GetMapping("save")
    public void save(@RequestBody Student student){
        studentRepository.saveOrUpate(student);
    }

    @GetMapping("update")
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpate(student);
    }

    @GetMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }

    @GetMapping("index")
    public String index(){
        return "当前端口:" + this.post;
    }
}
